package com.lap.watchdog.starter.core;

import java.time.Duration;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;

@RequiredArgsConstructor
public class RedisRepeat implements Repeat {

  private final StringRedisTemplate redisTemplate;

  @Override
  public boolean isDuplicate(String key, long period) {
    String cacheKey = getCacheKey(key);
    Boolean isAbsent =
        redisTemplate.opsForValue().setIfAbsent(cacheKey, "1", Duration.ofSeconds(period));

    return Boolean.FALSE.equals(isAbsent);
  }

  @Override
  public void delKey(String key) {
    redisTemplate.delete(getCacheKey(key));
  }
}
